package com.yuda.operationcenter.service.impl;

import java.util.List;

import com.yuda.common.core.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yuda.operationcenter.mapper.OperInformationMapper;
import com.yuda.operationcenter.domain.OperInformation;
import com.yuda.operationcenter.service.IOperInformationService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 资讯Service业务层处理
 *
 * @author wyn
 * @date 2021-04-12
 */
@Service
public class OperInformationServiceImpl implements IOperInformationService {
    @Autowired
    private OperInformationMapper operInformationMapper;


    /**
     * 查询资讯
     *
     * @param id 资讯ID
     * @return 资讯
     */
    @Override
    public OperInformation selectOperInformationById(Long id) {
        return operInformationMapper.selectOperInformationById(id);
    }

    /**
     * 查询资讯列表
     *
     * @param operInformation 资讯
     * @return 资讯
     */
    @Override
    public List<OperInformation> selectOperInformationList(OperInformation operInformation) {
        return operInformationMapper.selectOperInformationList(operInformation);
    }

    /**
     * 新增资讯
     *
     * @param operInformation 资讯
     * @return 结果
     */
    @Override
    public int insertOperInformation(OperInformation operInformation) {
        operInformation.setCreateTime(DateUtils.getNowDate());
        Integer maxOrderNo = operInformationMapper.MaxOrderNo();
        if (maxOrderNo == null) maxOrderNo = 0;
        operInformation.setOrderNo(maxOrderNo + 1);
        return operInformationMapper.insertOperInformation(operInformation);
    }

    /**
     * 修改资讯
     *
     * @param operInformation 资讯
     * @return 结果
     */
    @Override
    public int updateOperInformation(OperInformation operInformation) {
        operInformation.setUpdateTime(DateUtils.getNowDate());
        return operInformationMapper.updateOperInformation(operInformation);
    }

    /**
     * 批量删除资讯
     *
     * @param ids 需要删除的资讯ID
     * @return 结果
     */
    @Override
    public int deleteOperInformationByIds(Long[] ids) {
        return operInformationMapper.deleteOperInformationByIds(ids);
    }

    /**
     * 删除资讯信息
     *
     * @param id 资讯ID
     * @return 结果
     */
    @Override
    public int deleteOperInformationById(Long id) {
        return operInformationMapper.deleteOperInformationById(id);
    }

    @Override
    public int batchUpdateSellStatus(Long[] ids, int on_sell) {
        return operInformationMapper.batchUpdateSellStatus(ids, on_sell);
    }

    @Override
    public int removeImage(Long id) {

        //TODO 删除oss中图片
        return operInformationMapper.removeImage(id);
    }

    @Override
    public int checkIsCanUpdateStatus(Long id) {
        return operInformationMapper.checkIsCanUpdateStatus(id);
    }


    @Override
    public List<OperInformation> selectOperInformation_OrderNo_null(Long[] ids) {
        return operInformationMapper.selectOperInformation_OrderNo_null(ids);
    }

    @Override
    public List<OperInformation> selectOperInformation_OrderNo_Notnull() {
        return operInformationMapper.selectOperInformation_OrderNo_Notnull();
    }

    @Override
    public List<OperInformation> UnSelected(Long[] ids) {
        return operInformationMapper.UnSelected(ids);
    }

    @Override
    public int updateUnSelectedOrderNo(long id, int NewOrder) {
        return operInformationMapper.updateUnSelectedOrderNo(id, NewOrder);
    }

    @Override
    public Integer MaxOrderNo() {
        return operInformationMapper.MaxOrderNo();
    }


    @Override
    public int updateOrderNo(long id, int orderNo) {
        return operInformationMapper.updateOrderNo(id, orderNo);
    }

    @Transactional
    @Override
    public int updateOrderNo(long id1, int order2, long id, int orderNo) {
        int r1 = operInformationMapper.updateOrderNo(id, orderNo);
        int r2 = operInformationMapper.updateOrderNo(id1, order2);
        if (r1 == r2 && r1 == 1)
            return 1;
        return 0;
    }


    @Override
    public List<OperInformation> selectd(Long[] ids) {
        return operInformationMapper.selectd(ids);
    }

    @Override
    public OperInformation selectOne(long id) {
        return operInformationMapper.selectOne(id);
    }

    @Override
    public int updateOrder(long id) {
        return operInformationMapper.updateOrder(id);
    }

    @Override
    public OperInformation continuousOrder(long id, int size) {
        return operInformationMapper.continuousOrder(id, size);
    }

    @Override
    public OperInformation selectedByOrder(int orderNo) {
        return operInformationMapper.selectedByOrder(orderNo);
    }

    @Override
    public int updateOrder1(int orderNo1, int orderNo2) {
        return operInformationMapper.updateOrder1(orderNo1, orderNo2);
    }

    @Override
    public int updateOrder2(int orderNo1, int orderNo2) {
        return operInformationMapper.updateOrder2(orderNo1, orderNo2);
    }

    @Override
    public int orderAddOne(int minOrder, int maxOrder) {

        int existOrder = operInformationMapper.existOrder(maxOrder);
        if (existOrder == 0) {
            return 0;
        }

        //1.查询大的order 对应的Id
        OperInformation operInformation = operInformationMapper.selectedByOrderAndSellEq1(maxOrder);
        //把小的order 以上的 大的order 以下的 +1
        operInformationMapper.orderAddOne(minOrder, maxOrder);
        //把大的order 换成小的order
        int i = operInformationMapper.updateOrderNo(operInformation.getId(), minOrder);
        return i == 1 ? 1 : 0;
    }

    @Override
    public boolean existOrder(Integer orderNo) {
        return operInformationMapper.existOrder(orderNo) > 0;
    }

    @Override
    public OperInformation selectedByOrderAndSellEq1(int order1) {
        return operInformationMapper.selectedByOrderAndSellEq1(order1);
    }

    @Override
    public boolean checkIsUsed(Long[] ids) {

        return operInformationMapper.checkIsUsed(ids);
    }

    @Override
    public int batchUpdateSellStatus(Long id, int on_sell, int MaxOrderNo) {
        return operInformationMapper.batchUpdateSellStatusAndOrderNo(id, on_sell, MaxOrderNo);
    }

    @Override
    public List<OperInformation> selectByIds(Long[] ids) {
        return operInformationMapper.selectByIds(ids);
    }

}
